package com.cai.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.cai.constant.MessageConstant;
import com.cai.entity.PageResult;
import com.cai.entity.QueryPageBean;
import com.cai.entity.Result;
import com.cai.pojo.OrderSettingList;
import com.cai.service.OrderService;
import com.cai.service.OrderSettingListService;
import com.cai.service.SetmealService;
import com.cai.utils.DateUtils;
import com.cai.utils.SMSUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/orderSettingList")
public class OrderSettingListController {

    @Reference
    private OrderSettingListService orderSettingListService;
    @Reference
    private SetmealService setmealService;
    @Reference
    private OrderService orderService;

    /**
     * 分页
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
            try {
                return orderSettingListService.findPage(queryPageBean);
            }catch (Exception e){
                e.printStackTrace();
                return null;
            }
    }

    /**
     * 新增套餐
     * @param orderSettingList
     * @param setmealIds
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestBody OrderSettingList orderSettingList, @RequestParam("permissionIds") Integer[] setmealIds) throws Exception {
            return orderSettingListService.add(orderSettingList, setmealIds);
    }

    /**
     * 短信通知预约时间
     * @param orderSettingList
     * @return
     */
    @RequestMapping("/confirmReservation")
    public Result confirmReservation(@RequestBody OrderSettingList orderSettingList){
        //发短信
        try {
            Date orderDate = orderSettingList.getOrderDate();
            String stringDate = DateUtils.parseDate2String4Msg(orderDate);
            String phoneNumber = orderSettingList.getPhoneNumber();
            SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE,phoneNumber,stringDate);
            return new Result(true,MessageConstant.SEND_MESSAGE_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.SEND_MESSAGE_FAIL);
        }
    }

    /**
     * 取消预约1
     * @return
     */
    @RequestMapping("/cancerReservation")
    public Result cancerReservation(@RequestBody OrderSettingList orderSettingList){
        try {
            //删除数据
            orderSettingListService.del(orderSettingList.getId());
            //发短信
            String phoneNumber = orderSettingList.getPhoneNumber();
            SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE,phoneNumber,MessageConstant.CANCER_RESERVATION_SUCCESS);
            return new Result(true,MessageConstant.DELETE_ORDER_SUCCESS);
        }catch (Exception e){
            return new Result(false, MessageConstant.DELETE_ORDER_FAIL);
        }
    }


    /**
     * 批量取消预约
     * @param list
     * @return
     */
    @RequestMapping("/cancerReservationMore")
    public Result cancerReservationMore(@RequestBody List<OrderSettingList> list){
        try {
            for (OrderSettingList orderSettingList : list) {
                cancerReservation(orderSettingList);
            }
            return new Result(true,MessageConstant.DELETE_ORDER_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_ORDER_FAIL);
        }
    }


}
